<?php
namespace Acme\Test\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use RMD\AccessBundle\Form\Type\UserType;
use RMD\AccountBundle\Form\Type\SubscriberGroupType;
use RMD\AccountBundle\Form\Type\GroupTestType;
use RMD\AccountBundle\Form\Type\SubscriberType;
use RMD\AccountBundle\Entity\Subscriber;
use RMD\AccountBundle\Entity\Condition;
use RMD\AccountBundle\Entity\ListSegment;

use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Component\Yaml\Yaml;
class IndexController extends Controller
{
    public function testAction(Request $request) {
        //$groupSet = $this->getDoctrine()->getRepository('RMDAccountBundle:GroupSet')->find(14);
		//$form = $this->createForm(new GroupTestType(), $groupSet, array('em' => $this->getDoctrine()->getEntityManager()));
        $subscriber = $this->getDoctrine()->getRepository('RMDAccountBundle:Subscriber')->find(3);
		$form = $this->createForm(new SubscriberGroupType(), $subscriber, array('em' => $this->getDoctrine()->getEntityManager()));
		$originalGroups = array();
		foreach ($subscriber->getGroups() as $group) {
			$originalGroups[] = $group;
		}
		$types = $form->getTypes();
		$request = $this->getRequest();
		if ($request->isMethod('POST')) {
			$form->bind($request);
			$em = $this->getDoctrine()->getManager();
			
			//add groups
			foreach ($types[1]->getUnmappedFields() as $fieldName) {
				$data = $form->get($fieldName)->getData();
				if (is_array($data)) {				
					foreach ($data as $groupId) {
						$group = $this->getDoctrine()->getRepository('RMDAccountBundle:Group')->find($groupId);
						if (!$group || !$group->getGroupSet()->getList()->isOwnedBy($this->getUser())) {
							throw $this->createNotFoundException();
						}
						foreach ($originalGroups as $key => $origGroup) {	
							if ($origGroup->getId() === $groupId) {
								unset($originalGroups[$key]);
							}
						}
						$subscriber->addGroup($group);
					}
				} else {
					$group = $this->getDoctrine()->getRepository('RMDAccountBundle:Group')->find($data);
					if (!$group || !$group->getGroupSet()->getList()->isOwnedBy($this->getUser())) {
						throw $this->createNotFoundException();
					}
					foreach ($originalGroups as $key => $origGroup) {	
						if ($origGroup->getId() === $data) {
							unset($originalGroups[$key]);
						}
					}
					$subscriber->addGroup($group);
				}
			}
			//remove groups
			foreach ($originalGroups as $origGroup) {
				$subscriber->removeGroup($origGroup);
			}

			$em->persist($subscriber);
			$em->flush();
			return $this->redirect($this->generateUrl('acme_test_test'));
		}
        return $this->render('AcmeTest:Index:test.html.php', array('form' => $form->createView()));
        //$content = $this->renderView('AcmeTest:Index:test.html.php');

    //return new Response($content);
    }
    public function test2Action() {
		$condition = new Condition();
		$segment = new ListSegment('email');
		$form = $this->createForm(new \RMD\AccountBundle\Form\Type\TestType(), $condition, array('segmentOptions' => $segment->getOptions()));
		//var_dump(Yaml::parse(__DIR__ . '/../Resources/config/segmentRules.yml'));
		$request = $this->getRequest();
		/*if ($request->isMethod('POST')) {
			$form->bind($request);
			var_dump($condition);
		}*/
        return $this->render('AcmeTest:Index:test2.html.php', array('form' => $form->createView()));
    }
    public function test3Action($listid, $routeName) {
	$map = array(
		'account_lists' => 'Lists'
	);
        return $this->render('AcmeTest:Index:test3.html.php', array('map' => $map, 'route_name' => $routeName));
    }
    public function listMenuAction($listid, $routeName) {
		$map = array(
			'Subscribers' => array('account_subscribers', 'account_subscribers_add', 'account_subscribers_edit'),
			'Groups' => array('account_groups', 'account_groups_add', 'account_groups_edit'),
			'Settings' => array('account_lists_edit'),
		);
        return $this->render('RMDAccountBundle::listMenu.html.php', array('map' => $map, 'route_name' => $routeName, 'listid' => $listid));
    }
}


//$tool->updateSchema(array($em->getClassMetadata('RMD\AccessBundle\Entity\User')));
?>
